package edu.bnu.likun;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

//import sun.org.mozilla.javascript.internal.json.JsonParser;

/**
 * 鑾峰彇浜よ垂瑙勫垯
 */
@WebServlet(name="queryrulse",urlPatterns="/getrules")
public class QueryRules extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryRules() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		request.setCharacterEncoding("gbk");
//		response.setContentType("application/json;charSet=GBK");text/plain
		String driverName = getServletContext().getInitParameter("driver");
		String dbURL = getServletContext().getInitParameter("url"); //1433鏄暟鎹簱鐨勭鍙ｏ紝"li"鏄綘鐨勬暟鎹簱鍚嶇О
		String userName = getServletContext().getInitParameter("username"); //sa鏄暟鎹簱鐨勮秴绾х敤鎴凤紝鏈�濂戒笉瑕佹崲鍒殑鍚嶅瓧锛屾潈闄愰棶棰�
		String userPwd = getServletContext().getInitParameter("password"); //sa鐨勫瘑鐮�  
		response.setContentType("text/plain;charSet=gbk");
		
		Connection dbConn=null;
		try {
			  Class.forName(driverName).newInstance();
			  dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
			  Statement stmt=dbConn.createStatement();
			  ResultSet result=stmt.executeQuery("select content from u_rules");  
			  JsonArray ja=new JsonArray();  
			  int columns=result.getMetaData().getColumnCount();
			  ResultSetMetaData md=result.getMetaData();
			  while(result.next()){
				  JsonObject jo=new JsonObject();
				  for (int i = 1; i < columns+1; i++) {
					String name=md.getColumnName(i);
					String typename= md.getColumnTypeName(i);
					//System.out.println(name+":"+typename);
					if(typename.contains("int")){
					   jo.addProperty(name, result.getInt(i));
					}else{
						jo.addProperty(name, result.getString(i));
					}
				}
				  ja.add(jo);
		
			  }
			  JsonObject joresult=new JsonObject();
			  joresult.addProperty("code", 1);
			  joresult.add("data", ja);  
			  PrintWriter wrter=response.getWriter();
			  wrter.write(joresult.toString());
			  	  
			}
		catch (Exception e) {
		  e.printStackTrace();
		  JsonObject joresult=new JsonObject();
		  joresult.addProperty("code", 0);
		  joresult.addProperty("message", e.getMessage());		
		  PrintWriter wrter=response.getWriter();
		  wrter.write(joresult.toString());
		}finally{
		try {
		 if(dbConn!=null)
		   dbConn.close();
		} catch (SQLException e) {
		// TODO Auto-generated catch block
		  e.printStackTrace();
		  }
		  }
		}
		
	}